package com.example.administrator.szb.activity.supei;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.adapter.ProDeatilContentAdapter;
import com.example.administrator.szb.bean.ProjectDetail;
import com.example.administrator.szb.bean.SuPeiDetails;
import com.example.administrator.szb.common.CommonPost;
import com.example.administrator.szb.common.ReslutBean;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.ShareUtils;
import com.example.administrator.szb.util.Toasts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SuPeiDeatilActivity extends MVPBaseActivity {
    private TextView bar_title;
    private ImageView right_img;
    private LinearLayout ll_go_ucenter;
    private com.makeramen.roundedimageview.RoundedImageView img2;
    private TextView name2;
    private TextView gwgy;
    private com.example.administrator.szb.view.MyListView listView1;
    private TextView other_msg;
    private TextView need_msg;
    private CheckBox jgxq_check_sc;
    private LinearLayout ll_contacts;
    private ImageView iv_supei,right_sc;
    private TextView tv_title;
    private TextView tv_status;
    private LinearLayout ll_center2;
    private TextView center_content;
    private LinearLayout ll_center1,ll_bottoms;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv_3;
    private TextView tv_1_content;
    private TextView tv_2_content;
    private TextView tv_3_content;
    private LinearLayout ll_bottom;
    private ImageView clock;
    private TextView tips,tv_company,tv_bottom;
    private TextView tips_content;
    private int id = 0;
    private SuPeiDetails suPeiDetails;
    private View ll_statusa;
    private TextView project_status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_su_pei_deatil);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        ll_statusa=findViewById(R.id.ll_statusa);
        project_status=findViewById(R.id.project_status);
        tv_bottom = findViewById(R.id.tv_bottom);
        tv_company = findViewById(R.id.tv_company);
        ll_bottoms = findViewById(R.id.ll_bottoms);
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("项目详情");
        right_sc = findViewById(R.id.right_sc);
        right_sc.setVisibility(View.GONE);
        right_img = findViewById(R.id.right_img);
        id = getIntent().getIntExtra("id",0);
        ll_go_ucenter = (LinearLayout) findViewById(R.id.ll_go_ucenter);
        img2 = (com.makeramen.roundedimageview.RoundedImageView) findViewById(R.id.img2);
        name2 = (TextView) findViewById(R.id.name2);
        gwgy = (TextView) findViewById(R.id.gwgy);
        listView1 = (com.example.administrator.szb.view.MyListView) findViewById(R.id.listView1);
        other_msg = (TextView) findViewById(R.id.other_msg);
        need_msg = (TextView) findViewById(R.id.need_msg);
        jgxq_check_sc = (CheckBox) findViewById(R.id.jgxq_check_sc);
        ll_contacts = (LinearLayout) findViewById(R.id.ll_contacts);
        iv_supei = (ImageView) findViewById(R.id.iv_supei);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_status = (TextView) findViewById(R.id.tv_status);
        ll_center2 = (LinearLayout) findViewById(R.id.ll_center2);
        center_content = (TextView) findViewById(R.id.center_content);
        ll_center1 = (LinearLayout) findViewById(R.id.ll_center1);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_1_content = (TextView) findViewById(R.id.tv_1_content);
        tv_2_content = (TextView) findViewById(R.id.tv_2_content);
        tv_3_content = (TextView) findViewById(R.id.tv_3_content);
        ll_bottom = (LinearLayout) findViewById(R.id.ll_bottom);
        clock = (ImageView) findViewById(R.id.clock);
        tips = (TextView) findViewById(R.id.tips);
        tips_content = (TextView) findViewById(R.id.tips_content);
        tv_1.setVisibility(View.GONE);
        tv_1_content.setVisibility(View.GONE);
        tv_2.setVisibility(View.GONE);
        tv_2_content.setVisibility(View.GONE);
        tv_3.setVisibility(View.GONE);
        tv_3_content.setVisibility(View.GONE);
    }

    @Override
    protected void initEvent() {
        right_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CheckInfoUtils.isLogin(context)){
                    right_sc.setEnabled(false);
                    CommonPost.doSc(activity, 1, id, new CommonPost.OnShouCangListener() {
                        @Override
                        public void onSuccess(ReslutBean bean) {
                            right_sc.setEnabled(true);
                            Toasts.show(context,bean.getStr());
                            if (bean.getCode()==0){
                                right_sc.setImageResource(R.mipmap.sc_white_no);
                            }else {
                                right_sc.setImageResource(R.mipmap.sc_white_yes);

                            }
                        }

                        @Override
                        public void onFail(String str) {
                            right_sc.setEnabled(true);
                        }
                    });
                }
            }
        });
        ll_contacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckInfoUtils.check(activity, iosDiaolog, ll_bottom, new CheckInfoUtils.OnSuccessLietener() {
                    @Override
                    public void onSuccess() {
                        if (suPeiDetails!=null){
                            String tel = suPeiDetails.getData().get(0).getServicetel();
                            QTLog.e(tel);
                            if (!TextUtils.isEmpty(tel)){
                                QTLog.e(tel);
                                CommonPost.callPhone(activity,id,0);
                                QTLog.e(tel);
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                Uri data = Uri.parse("tel:" + tel);
                                intent.setData(data);
                                startActivity(intent);
                            }
                        }
                    }
                });




            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (suPeiDetails==null){
                    return;
                }
                /*Intent intent = new Intent(context, JGXQActivity.class);
                intent.putExtra("id", suPeiDetails.getData().get(0).getUser_id());
                startActivity(intent);*/
            }
        });
        right_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(suPeiDetails!=null){
                    //分享
                    ShareUtils.share(SuPeiDeatilActivity.this,null,URLAddress.SUPEI_URL+id+"&app=1","项目详情",suPeiDetails.getData().get(0).getTitle());
                }

            }
        });
        jgxq_check_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jgxq_check_sc.setEnabled(false);
                if (!CheckInfoUtils.isLogin(context)){
                    jgxq_check_sc.setEnabled(true);
                    jgxq_check_sc.setChecked(false);
                    return;
                }
                //DialogUtil.showIsoProgressbar(context,"正在加载");
                CommonPost.doSc(activity, 1, id, new CommonPost.OnShouCangListener() {
                    @Override
                    public void onSuccess(ReslutBean bean) {
                       // DialogUtil.dialog_ios.dismiss();
                        jgxq_check_sc.setEnabled(true);
                        Toasts.show(context,bean.getStr());
                        if (bean.getCode()==0){
                            jgxq_check_sc.setChecked(false);
                        }else {
                            jgxq_check_sc.setChecked(true);
                        }
                    }

                    @Override
                    public void onFail(String str) {
                        //DialogUtil.dialog_ios.dismiss();
                        jgxq_check_sc.setEnabled(true);
                        if (jgxq_check_sc.isChecked()){
                            jgxq_check_sc.setChecked(false);
                        }else {
                            jgxq_check_sc.setChecked(true);
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
        getInfo();
    }
    private void getInfo(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        HttpUtils.post2(activity,URLAddress.SUPEI_DEATIL, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {

                str = str.replace("\"0\":[]","\"0\":{}");
                str = str.replace("\"1\":","\"first\":");
                str = str.replace("\"0\":","\"zero\":");

                suPeiDetails = JSON.parseObject(str,new TypeReference<SuPeiDetails>(){});
                if (suPeiDetails.getData().get(0).getUser_id()== SPUtils.getUserId()){
                    ll_bottoms.setVisibility(View.GONE);
                }

                if (suPeiDetails.getData().get(0).getStatus()==3||suPeiDetails.getData().get(0).getStatus()==4){
                    ll_bottoms.setVisibility(View.GONE);
                    tv_bottom.setVisibility(View.VISIBLE);
                }
                if (suPeiDetails.getData().get(0).getStatus()==2){
                    ll_statusa.setVisibility(View.VISIBLE);
                    project_status.setText(suPeiDetails.getData().get(0).getReason());

                }
                QTLog.e(suPeiDetails.getData().get(0).getServicetel());
                Glide.with(context).load(URLAddress.HOST_PORT+suPeiDetails.getMsginfo().getHeadimg()).error(R.mipmap.default_header_s).into(img2);
                String[] name = suPeiDetails.getMsginfo().getReal_name().split("-");
                name2.setText(name[0]);
                if (name.length>1){
                    name2.setText(name[0]+name[1]);
                }

                String timesarr[] = suPeiDetails.getData().get(0).getEnd_time().split(" ");
                tips_content.setText(timesarr[0]);
                String timesarr2[] = suPeiDetails.getData().get(0).getC_time().split(":");
                String strs = timesarr2[0]+":"+timesarr2[1];
                if (!TextUtils.isEmpty(suPeiDetails.getMsginfo().getCompany()))
                tv_company.setText(suPeiDetails.getMsginfo().getCompany());
                gwgy.setText("项目发布时间："+strs);
                if (!Contants.isEmpty(suPeiDetails.getData().get(0).getOther_require()))
                need_msg.setText(suPeiDetails.getData().get(0).getOther_require());
                tv_title.setText(suPeiDetails.getData().get(0).getTitle());
                if (suPeiDetails.getData().get(0).getStatus()==0){
                    tv_status.setText("待审核");
                    tv_status.setTextColor(Color.parseColor("#0096FF"));
                    tv_status.setBackgroundColor(Color.parseColor("#D7EBFA"));
                }
                if (suPeiDetails.getData().get(0).getStatus()==1){
                    tv_status.setText("进行中");
                    tv_status.setTextColor(Color.parseColor("#0096FF"));
                    tv_status.setBackgroundColor(Color.parseColor("#D7EBFA"));
                }
                if (suPeiDetails.getData().get(0).getStatus()==2){
                    tv_status.setText("未通过");
                    tv_status.setTextColor(Color.parseColor("#ffffff"));
                    tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
                }
                if (suPeiDetails.getData().get(0).getStatus()==3){
                    tv_status.setText("已结束");
                    tv_status.setTextColor(Color.parseColor("#ffffff"));
                    tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
                }
                List<ProjectDetail.YWInfoData> list = new ArrayList();
                Map<String,String> listMapTmp = new HashMap<String, String>();
                for (Map.Entry<String,String> e : suPeiDetails.getData().get(0).getZero().entrySet()){
                    ProjectDetail.YWInfoData keyValue = new ProjectDetail.YWInfoData(e.getKey(),e.getValue());
                    list.add(keyValue);

                    if (suPeiDetails.getData().get(0).getMold()==2){
                        listMapTmp.put("服务类型",suPeiDetails.getData().get(0).getTitle()+"-"+e.getValue());
                    }else {
                        listMapTmp.put(e.getKey(),e.getValue());
                    }
                }
                if (suPeiDetails.getData().get(0).getFirst().size()==0&&suPeiDetails.getData().get(0).getZero().size()>0){

                    suPeiDetails.getData().get(0).getFirst().add(listMapTmp);
                }
                if (suPeiDetails.getData().get(0).getFirst().size()==0&&suPeiDetails.getData().get(0).getZero().size()==0){
                    listMapTmp.put("项目信息","-");
                    suPeiDetails.getData().get(0).getFirst().add(listMapTmp);
                }

                for (int i = 0; i < suPeiDetails.getData().get(0).getFirst().size(); i++) {
                    Map<String,String> listMap = suPeiDetails.getData().get(0).getFirst().get(i);
                    switch (i){
                        case 0:
                            for (Map.Entry<String,String> entry:listMap.entrySet()){
                                tv_1.setText(entry.getValue());
                                tv_1_content.setText(entry.getKey());
                                tv_1.setVisibility(View.VISIBLE);
                                tv_1_content.setVisibility(View.VISIBLE);
                            }

                            break;
                        case 1:
                            for (Map.Entry<String,String> entry:listMap.entrySet()){
                                tv_2.setText(entry.getValue());
                                tv_2_content.setText(entry.getKey());
                                tv_2.setVisibility(View.VISIBLE);
                                tv_2_content.setVisibility(View.VISIBLE);
                            }

                            break;
                        case 2:
                            for (Map.Entry<String,String> entry:listMap.entrySet()){
                                tv_3.setText(entry.getValue());
                                tv_3_content.setText(entry.getKey());
                                tv_3.setVisibility(View.VISIBLE);
                                tv_3_content.setVisibility(View.VISIBLE);
                            }

                            break;
                    }
                    //info = info + map.get(dataBean.getBusformId().get(i)).getKeyName() +":"+ map.get(dataBean.getBusformId().get(i)).getValueName() +"     ";
                }
                listView1.setAdapter(new ProDeatilContentAdapter(list,context));

                if (suPeiDetails.getData().get(0).getCollects()==1){
                    jgxq_check_sc.setChecked(true);
                }

            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }

    /**
     * 结束项目
     */
    private void endProject(int sp_id){
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("id",sp_id);
        HttpUtils.post(activity, URLAddress.SUPEI_END, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                Toasts.show(context,str);
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
}
